# -*- coding: utf-8 -*-
"""
Module Description:
"""
# from pyshare.core.context import ActionContext
# from pyshare.framework.firefly_dc.global_errcode import GlobalErrCode
# from pyshare.framework.interceptor.base_interceptor import BaseInterceptor
# from pyshare.framework.firefly_dc.action_service import ActionService
# from common.zk_config import ServerStatus, CrossServerStatus
# from common.service_config import ServerRunMode, env_app_id
# from pyshare.const.lang.tips_info import TipsInfo
from framework.firefly_dc.context import ActionContext
from framework.interceptor.base_interceptor import BaseInterceptor


class ContextVerifyInterceptor(BaseInterceptor):
    """区服服务上下文检验"""

    @staticmethod
    def pre_process(request, response):
        # TODO
        old_app_id = request.get_attribute('appid', default=0)
        logical_region_id = request.get_attribute('logical_region_id', default=0) \
                            or request.get_attribute('logicalregionid', default=0)
        app_id = int(old_app_id) or int(logical_region_id)

        # server_id = request.get_attribute('serverid')

        # # 要求传入区服编号
        # if server_id is None or not str(server_id).isdigit():
        #     response.set_info(info=TipsInfo.GlobalKeys.pleaseInputServerId, stat=GlobalErrCode.ERROR)
        #     return False
        #
        # # 要求传入appid
        # if app_id is None or not str(app_id).isdigit():
        #     response.set_info(info=TipsInfo.GlobalKeys.pleaseInputAppId, stat=GlobalErrCode.ERROR)
        #     return False

        # server_id = int(server_id)

        # # 集群模式下的特殊判定
        # if ServerRunMode.is_cluster_mode():
        #     # 要求传入的应用编号与环境变量中的应用编号一致
        #     if env_app_id is not None and app_id != env_app_id:
        #         response.set_info(info=TipsInfo.GlobalKeys.wrongAppId, stat=GlobalErrCode.ERROR)
        #         return False
        #
        #     # 要求指定的应用区服处于可访问状态
        #     if not ServerStatus.is_server_alive(app_id, server_id):
        #         # 是否不限制区服状态能够访问该接口
        #         action_class = ActionService.get_target(request.action_id)
        #         if action_class and not action_class.can_call_when_server_stat_offline:
        #             response.set_info(info=TipsInfo.GlobalKeys.serviceMaintenanceRefuseAccess,
        #                               stat=GlobalErrCode.SERVER_MAINTAINING)
        #             return False

        # 构造上下文对象
        ctx = ActionContext.create_by_request_response(request=request, response=response)
        # 绑定请求对象和上下文对象
        request.ctx = ctx

        return True


# class CrossContextVerifyInterceptor(BaseInterceptor):
#     """跨服服务上下文检验"""
#
#     @staticmethod
#     def pre_process(request, response):
#         # TODO
#         old_app_id = request.get_attribute('appid', default=0)
#         logical_region_id = request.get_attribute('logical_region_id', 0)
#         app_id = int(old_app_id) or int(logical_region_id)
#
#         server_id = request.get_attribute('serverid')
#
#         # 要求传入区服编号
#         if server_id is None or not (str(server_id).isdigit() or server_id == 'cross'):
#             response.set_info(info=TipsInfo.GlobalKeys.pleaseInputServerId, stat=GlobalErrCode.ERROR)
#             return False
#
#         # 要求传入appid
#         if app_id is None or not str(app_id).isdigit():
#             response.set_info(info=TipsInfo.GlobalKeys.pleaseInputAppId, stat=GlobalErrCode.ERROR)
#             return False
#
#         # 集群模式下的特殊判定
#         if ServerRunMode.is_cluster_mode():
#             # 要求传入的应用编号与环境变量中的应用编号一致
#             if env_app_id is not None and app_id != env_app_id:
#                 response.set_info(info=TipsInfo.GlobalKeys.wrongAppId, stat=GlobalErrCode.ERROR)
#                 return False
#
#             # TODO 暂时屏蔽，考虑到访问跨服应用的都是区服服务器或后台
#             # # 要求指定的应用区服处于可访问状态
#             if not CrossServerStatus.is_cross_alive(app_id):
#                 # 是否不限制区服状态能够访问该接口
#                 action_class = ActionService.get_target(request.action_id)
#                 if action_class and not action_class.can_call_when_server_stat_offline:
#                     response.set_info(info=TipsInfo.GlobalKeys.CrossRefuseAccess,
#                                       stat=GlobalErrCode.SERVER_MAINTAINING)
#                     return False
#
#         # 构造上下文对象
#         ctx = ActionContext.create_by_request_response(request=request, response=response)
#         # 绑定请求对象和上下文对象
#         request.ctx = ctx
#
#         return True
